<?php

class Manage extends Controller {

	function Manage()
	{
		parent::Controller();
		$this->load->library('access');
		
		$this->page->css_link("style.css");	
		$this->load->helper('debug');
		$this->load->library('validation');
		
		$this->load->helper('url');
	
	}
	
	function index()
	{
		if (!$this->access->logged_in())redirect('manage/login');
	    $q=$this->db->get('site');
	    $data['result']=$q->result();
		$this->load->view('admin',$data);
	}
	
	function add()
	{
	    $this->load->helper('form');
		
		if ( ! $this->_validate_form() )
		{
			//echo $pid;
		   $data['id']=$pid;
		   $q=$this->db->get('site');
		   $this->load->view('add',$data);
		}
		else
		{

			$data= array(
			    "name"=>$this->input->post('name'),
			    "site_url"=>$this->input->post('site_url'),
			    "feed_url"=>$this->input->post('feed_url')
			);
			$this->db->insert('site',$data);
			$this->session->set_flashdata('msg', 'Category Created');
			redirect('manage/edit/'.$pid);
		}
	}
	
	function edit($id=0)
	{
	   $this->load->helper('form');
		
		if ( ! $this->_validate_form() )
		{
			//echo $pid;
		   $data['id']=$pid;
		   $this->db->where('site_id',$id);
		   $q=$this->db->get('site');
		   $data['data']=$q->row();

		   $this->load->view('edit',$data);
		}
		else
		{

			$data= array(
			    "name"=>$this->input->post('name'),
			    "site_url"=>$this->input->post('site_url'),
			    "feed_url"=>$this->input->post('feed_url')			
			);
			$this->db->where('site_id',$id);
			$this->db->update('site',$data);
			$this->session->set_flashdata('msg', 'Category Created');
			redirect('manage/edit/'.$id);
		}
	}
	
	function remove($id=0)
	{
	    $this->db->where('site_id',$id);
		$this->db->delete('site');
			
	}
	function login()
	{
		$this->access->restrict('logged_out');

		$this->load->library('validation');
		$this->load->helper('form');

		$rules = array(
				'username'	=>	'trim|required|strip_tags|xss_clean',
				'password'	=>	'trim|required|check_login' 
		);
		
		$this->validation->set_rules($rules);
		
		$fields = array(
				'username'	=>	'username',
				'password'	=>	'password'
		);
		
		$this->validation->set_fields($fields);
		if ($this->validation->run() == FALSE)
		{
					
			$this->session->keep_flashdata('referrer');
			$this->load->view('login');
		}
		else
		{
			
		    $uri = $this->session->flashdata('referrer') ? $this->session->flashdata('referrer') : 'manage';
			redirect($uri);
		}
	}
	function logout()
	{
		
		$this->access->logout();
		redirect('');
	}
	
    function _validate_form($old_name = '')
	{
		$rules = array(
				'name'			=>	'trim|required||max_length[100]',
		        'site_url'		=>  'trim|required',
				'feed_url'		=>  'trim|required',

		);
		$this->validation->set_rules($rules);
		
		$fields = array(
				'name'			=>	'Name',
		        'site_url'		=>  'Site Url',
	
		);
		$this->validation->set_fields($fields);
		
		return ($this->validation->run() == FALSE) ? FALSE : TRUE;
	}
}
?>